$().ready(function() {
	get_hash();
	$("#dateNoteDate, #adddateNoteDate").datepicker({ 
		changeYear: true,
	});	
	$("#submitCheckuplog").click(function(){
		saveCheckuplog();
	});
	$("#add_checkuplog_form").click(function(){
		addCheckuplogForm();
	});
	$("#addsubmitCheckuplog").click(function(){
		addCheckuplog();
	});
});
function list_checkuplog(){
	var asort = 0;
	checkuplog_icons = {};
	xfunc = "checkuplog/list_checklupog";
	
	data_sent = {};
		
		$.ajax({ 
			type: "POST", // request type POST
			url: base_url+xfunc, // post to PHP file
			data: data_sent, // request send to PHP file
			dataType:'json',
			success: function(result){ // result = return from php request
				$(result.intID).each(function(k,v){
		    		//get_userName(result.strCreatedBy[k]);
					asort = result.intID[k];
					checkuplog_icons_panel = '';
						checkuplog_icons_panel +='<tr>';
							checkuplog_icons_panel +='<td><span class="username">'+result.strUserName[k]+'</span></td>';
							checkuplog_icons_panel +='<td width="130">'+result.strNoteTitle[k]+'</td>';
							checkuplog_icons_panel +='<td width="90">'+result.dateNoteDate[k]+'</td>';
							checkuplog_icons_panel +='<td class="controls"><div class="btn-group">';
							checkuplog_icons_panel +='<button class="btn btn-primary" onclick="javascript:editCheckuplog(\''+result.intID[k]+'\')"><i class="icon-edit"></i>Edit</button>';
							checkuplog_icons_panel +='<button class="btn btn-danger" onclick="javascript:delCheckuplog(\''+result.intID[k]+'\', \''+result.strNoteTitle[k]+'\')"><i class="icon-trash"></i>Delete</button>';
							checkuplog_icons_panel +='</td>';
						checkuplog_icons_panel +='</tr>';
					checkuplog_icons[result.intID[k]]=checkuplog_icons_panel;
				});

				checkuplog_html_panel = '<table class="table table-striped table-bordered" id="checkuplog_table">';
				checkuplog_html_panel += '<thead>';
					checkuplog_html_panel += '<tr>';
						checkuplog_html_panel += '<th width="100">Username</th>';
						checkuplog_html_panel += '<th>Title</th>';
						checkuplog_html_panel += '<th>Date Note</th>';
						checkuplog_html_panel += '<th>Control</th>';
					checkuplog_html_panel += '</tr>';
				checkuplog_html_panel += '</thead>';
				checkuplog_html_panel += '<tbody>';
				for(x=0;x<=asort;x++){
					if($.trim(checkuplog_icons[x])!=""){
						checkuplog_html_panel +=checkuplog_icons[x];
					}			
				}		
				checkuplog_html_panel += '</tbody>';		
				checkuplog_html_panel += '</table>';
				
				$('#checkuplog_content').html(checkuplog_html_panel);
				$('#checkuplog_table').dataTable({
					"sPaginationType": "full_numbers",
					"bLengthChange": false,
					"bSort": false
				});
			}
		});	
}
function addCheckuplogForm(){
	$("#checkuplog_content, #add_checkuplog_form").hide();
	$("#addCheckuplog").slideDown('slow', function(){
		$("#checkuplog_content").hide();
		$("#addCheckuplog").slideDown('slow', function(){
			$(".back-btn-addcheckuplog a").attr("onclick","btn_back_addcheckuplog()");
		});	
	});	
}
function addCheckuplog(){
	var intUserID = $("#intUserID").val();
	var strNoteTitle = $("#addstrNoteTitle").val();
	var dateNoteDate = $("#adddateNoteDate").val();
	var strNotes = $("#addstrNotes").val();
	if(strNoteTitle==''){
		$("#errorCheckuplog").html("<div class='alert alert-error'>Error: Put the title!</div>");
	} else if(dateNoteDate=='') {
		$("#errorCheckuplog").html("<div class='alert alert-error'>Error: Put the date!</div>");
	} else if(strNotes=='') {
		$("#errorCheckuplog").html("<div class='alert alert-error'>Error: Put the Notes!</div>");
	} else {
		dateNote = dateNoteDate.split("/");
		adddateNoteDate = dateNote[2]+'-'+dateNote[0]+'-'+dateNote[1];
		xfunc = "checkuplog/save_checkuplog";
		data_sent = {
				'intUserID':intUserID,
				'strNoteTitle':strNoteTitle,
				'dateNoteDate':adddateNoteDate,
				'strNotes':strNotes
		};
		$.ajax({
			type: 'POST',
			url: base_url+xfunc,
			data: data_sent, 
			dataType:'json',
			success: function(result){
				$("#errorCheckuplog").hide();
				$("#addcheckuplogSuccess").html('<div class="alert alert-success">Data was successfully saved!</div>');

				$("#intUserID").val('');
				$("#addstrNoteTitle").val('');
				$("#adddateNoteDate").val('');
				$("#addstrNotes").val('');
				$(".alert-success").fadeOut(5000);
			}
		});
	}
}
function editCheckuplog(noteID){
	xfunc = "checkuplog/get_checklupog";
	data_sent = {
			'intID':noteID
	};
	$.ajax({
		type: 'POST',
		url: base_url+xfunc,
		data: data_sent, 
		dataType:'json',
		success: function(result){
			dateNote = result.dateNoteDate.split("-");
			dateNoteDate = dateNote[1]+'/'+dateNote[2]+'/'+dateNote[0];
			$("#intChekuplogID").val(result.intID);
			$("#intUserID").val(result.intUserID);
			$("#strNoteTitle").val(result.strNoteTitle);
			$("#dateNoteDate").val(dateNoteDate);
			$("#strNotes").val(result.strNotes);
		}
	});

	$("#add_checkuplog_form").hide();
	$("#checkuplog_content").hide();
	$("#editCheckuplog").slideDown('slow', function(){
		$(".back-btn-checkuplog a").attr("onclick","btn_back_checkuplog()");
	});	
}
function saveCheckuplog(){
	var intID = $("#intChekuplogID").val();
	var intUserID = $("#intUserID").val();
	var strNoteTitle = $("#strNoteTitle").val();
	var dateNoteDate = $("#dateNoteDate").val();
	var strNotes = $("#strNotes").val();

	dateNote = dateNoteDate.split("/");
	dateNoteDate = dateNote[2]+'-'+dateNote[0]+'-'+dateNote[1];
	xfunc = "checkuplog/save_checkuplog";
	data_sent = {
			'intID':intID,
			'intUserID':intUserID,
			'strNoteTitle':strNoteTitle,
			'dateNoteDate':dateNoteDate,
			'strNotes':strNotes
	};
	
	$.ajax({
		type: 'POST',
		url: base_url+xfunc,
		data: data_sent, 
		dataType:'json',
		success: function(result){
			$("#checkuplogSuccess").html('<div class="alert alert-success">Data was successfully updated!</div>');
			$(".alert-success").fadeOut(5000);
		}
	});
}
function delCheckuplog(noteID, titleName){
	xfunc = "checkuplog/delete_checklupog";
	data_sent = {
			'intID':noteID
	};
	$('#label-delete').html(titleName);
    $('#deleteModal').modal('show');
    $('button#confirm').click(function(e){
        $('#deleteModal').modal('hide');
		
		$.ajax({
			type: 'POST',
			url: base_url+xfunc,
			data: data_sent, 
			dataType:'json',
			success: function(result){
				list_checkuplog();
			}
		});
    });
}
function btn_back_addcheckuplog(){
	$("#addCheckuplog").hide();
	$("#checkuplog_content").slideDown('slow', function(){
		$("#add_checkuplog_form").show();
		list_checkuplog();
	});	
}

function btn_back_checkuplog(){
	$("#editCheckuplog").hide();
	$("#checkuplog_content").slideDown('slow', function(){
		$("#add_checkuplog_form").show();
		list_checkuplog();
	});	
}
